package com.huiyeji.hui.service.impl;

import com.huiyeji.hui.constant.RelationConstant;
import com.huiyeji.hui.entity.UserUserRelation;
import com.huiyeji.hui.exception.HuiException;
import com.huiyeji.hui.mapper.UserUserRelationMapper;
import com.huiyeji.hui.service.UserRelationService;
import com.huiyeji.hui.utils.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhaoduo
 */
@Service
public class UserRelationServiceImpl implements UserRelationService {

    @Autowired
    private UserUserRelationMapper mapper;

    @Override
    public List<Long> getUserRelationUserIds(Long uid) {
        return mapper.getUserRelationUserIds(uid);
    }

    @Override
    public void deleteFriend(Long uid, Long fid) {
        int row = mapper.delete(uid,fid);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void careFriend(Long uid, Long fid) {
        int row1 = mapper.resetCareFriend(uid,RelationConstant.USER_RELATION_CARE,RelationConstant.USER_RELATION_NORMAL);
        int row = mapper.changeRelation(uid,fid, RelationConstant.USER_RELATION_CARE);
        if (row != 1){
            throw new HuiException(ErrorEnum.DELETE_CARE_FRIEND);
        }
    }

    @Override
    public void putFriendIntoBlackList(Long uid, Long fid) {
        int row = mapper.changeRelation(uid,fid,RelationConstant.USER_RELATION_BLACK);
        if (row != 1){
            throw new HuiException(ErrorEnum.PUT_BLACK_LIST_ERROR);
        }
    }

    @Override
    public void changeRemark(Long uid, Long oid, String remark) {
        int row = mapper.changeRemark(uid,oid,remark);
        if (row != 1){
            throw new HuiException(ErrorEnum.MODIFY_ERROR);
        }
    }

    @Override
    public void recoverNormalRelation(Long uid, Long oid) {
        int row = mapper.changeRelation(uid,oid,RelationConstant.USER_RELATION_NORMAL);
        if (row != 1){
            throw new HuiException(ErrorEnum.RECOVER_NORMAL_RELATION_ERROR);
        }
    }

    @Override
    public void addRelation(UserUserRelation relation) {
        int row = mapper.insertSelective(relation);
        Long temp = relation.getUid1();
        relation.setUid1(relation.getUid2());
        relation.setUid2(temp);
        int row1 = mapper.insertSelective(relation);
    }

}
